package com.qf.dao.impl;

import com.qf.dao.Bookdao;
import com.qf.entity.Book;
import com.qf.entity.Page;
import com.qf.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Wy
 * @version V1.0
 * @Project day41-3
 * @Package com.qf.dao.impl
 * @Description:
 * @Date 2022/5/14 13:15
 */
public class BookDaoImpl implements Bookdao {
    private QueryRunner runner=new QueryRunner(DBUtil.getDateSource());
    @Override
    public List<Book> selectAll() {
        String sql="select * from t_book";
        try {
        return    runner.query(sql,new BeanListHandler<>(Book.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addOne(Book book) {
        String sql="insert into t_book values(null,?,?,?,?,?)";
        try {
        return    runner.update(sql,book.getName(),book.getPrice(),book.getAuthor(),book.getPublisher(),book.getDescription());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteBook(String bookId) {
        String sql="delete from t_book where id = ?";
        try {
            runner.update(sql,bookId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int uppdateBook(Book book, String bookIds) {
        String sql="update t_book set  name=?,price=?,description=?,author=?,publisher=? where id=?";
        try {
       return   runner.update(sql,book.getName(),book.getPrice(),book.getAuthor(),book.getPublisher(),book.getDescription(),bookIds);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Book selectOne(String bookId) {
          String sql="select * from t_book where id = ?";
        try {
          return   runner.query(sql,new BeanHandler<>(Book.class),bookId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int selectAllByPageCount() {
        String sql="select count(*) from t_book";
        try {
            long query = runner.query(sql, new ScalarHandler<Long>());
              return (int)query;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public List<Book> selectAllByPage(Page page) {
        String sql="select * from t_book limit ? , ?";
        try {
         return    runner.query(sql,new BeanListHandler<Book>(Book.class),page.getStartRows(),page.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
